﻿#include <assert.h>
#include "loop_controller.h"
#include "tcp_server.h"
#include "tcp_client.h"
static void sig_deal(int signo);
tcp_server tcpserver;
int cnt = 0;
#if TCP_SERVER
int main(int argc, char** argv)
{
	if (signal(SIGINT, sig_deal) == SIG_ERR)
		printf("can't catch SIGINT\n");
	if (signal(SIGTERM, sig_deal) == SIG_ERR)
		printf("can't catch SIGTERM\n");
	if (signal(SIGSEGV, sig_deal) == SIG_ERR)
		printf("can't catch SIGSEGV\n");
	
	bool bpen=tcpserver.open_listen("0.0.0.0",8011);
	assert(!bpen);
	tcpserver.on_new_conn([&](tcp_connect* tcpconn){
		cnt++;
		printf("client cnt %d\n", cnt);
	});
	tcpserver.on_bytes_call([](tcp_connect* tcpconn, char* buf, int bsize){
		printf("%s\n", buf);
		tcpconn->write_bytes(buf,bsize);
	});

	tcpserver.on_conn_close([](tcp_connect* tcpconn){

	});
	g_loop_contor.start_run();	

	return 0;
}
#endif
static void sig_deal(int signo)
{
	switch (signo)
	{	
	case SIGINT:
		printf("catch SIGINT, Process Exit\n");		
		tcpserver.close_listen();
		g_loop_contor.stop_run();
		break;
	case SIGTERM:
		printf("catch SIGTERM, Process Exit\n");				
		g_loop_contor.stop_run(); 
		exit(0);
		break;
	case SIGSEGV:
		break;
	default:
		printf("catch SIG %d, Process Maybe core\n", signo);		
		signal(signo, SIG_DFL);
		break;
	}
}

